/**
 * Copyright &copy; 2012-2014 <a href="https://github.com.jims.base">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.patient.health.service;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.health.dao.GeneticDictDao;
import com.jims.master.modules.patient.health.entity.GeneticDict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 遗传病Service
 * @author xueyx
 * @version 2017-07-20
 */
@Service
@Transactional(readOnly = true)
public class GeneticDictService extends CrudService<GeneticDictDao, GeneticDict> {


	@Override
    public Page<GeneticDict> findPage(Page<GeneticDict> page, GeneticDict geneticDict) {
		return super.findPage(page, geneticDict);
	}

	@Transactional(readOnly = false)
	public void update(String oldId, String newId) {
		// 根据id更新遗传病的权值
		String arr[] = oldId != null ? oldId.split(",") : null;
		String arr1[] = newId != null ? newId.split(",") : null;

		doUpdate(arr, -1);
		doUpdate(arr1, 1);
	}

	private void doUpdate(String arr[], int val) {
		GeneticDict geneticDict = new GeneticDict();
		if (arr == null) return;
		for (int i = 0; i < arr.length; i++) {
			if (!StringUtils.isNumeric(arr[i])) break;
			geneticDict.setId(arr[i]);
			geneticDict.setWeight(String.valueOf(val));
			super.save(geneticDict);
		}
	}

}